package com.example.dense_rack_server.controller;

public class Cellid {
    int mid;
//    byte[] uid = new byte[5];

    public Cellid(int mid) {
        this.mid = mid;
    }

    public Cellid(int region, int lie, int dir, int jie, int ceng, int position) {
        this.mid = (region) * 10000000 + lie * 100000 + dir * 10000 + jie * 1000 + ceng * 100 + position;
    }

    public int getId() {
        return mid;
    }

    public void setId(int mid) {
        this.mid = mid;
    }

    public int getRegion() {
        int region = this.mid / 10000000;
        if (region < 0) {
            region = 0;
        }
        return region;
    }

    public void setRegion(int region) {
        int tail = this.mid % 10000000;
        this.mid = tail + (region) * 10000000;
    }

    public int getLie() {
        int tem = this.mid % 10000000;
        return tem / 100000;
    }

    public void setLie(int lie) {
        int header = this.mid / 10000000;
        int tail = this.mid % 100000;
        int current = lie * 100000;
        this.mid = header * 10000000 + tail + current;
    }

    public int getDir() {
        int tem = this.mid % 100000;
        return tem / 10000;
    }

    public void setDir(int dir) {
//        this.mid = (region+10)*10000000 + lie*100000 + dir*10000 + jie*1000 + ceng*100 + subid;
        //    10 12 1 1 1 12
        int header = this.mid / 100000;
        int tail = this.mid % 10000;
        int current = dir * 10000;
        this.mid = header * 100000 + tail + current;
    }

    public int getJie() {
        int tem = this.mid % 10000;
        return tem / 1000;
    }

    public void setJie(int jie) {
//        this.mid = (region+10)*10000000 + lie*100000 + dir*10000 + jie*1000 + ceng*100 + subid;
        //    10 12 1 1 1 12
        int header = this.mid / 10000;
        int tail = this.mid % 1000;
        int current = jie * 1000;
        this.mid = header * 10000 + tail + current;
    }

    public int getCeng() {
        int tem = this.mid % 1000;
        return tem / 100;
    }

    public void setCeng(int ceng) {
        int header = this.mid / 1000;
        int tail = this.mid % 100;
        int current = ceng * 100;
        this.mid = header * 1000 + tail + current;
    }

    public int getPosition() {
        return mid % 100;
    }

    public void setPosition(int subid) {
        int header = this.mid / 100;
        this.mid = header * 100 + subid;
    }

    public int iterNext() {
        this.setPosition(this.getPosition() + 1);
        if (this.getPosition() == 17) {
            this.setCeng(this.getCeng() + 1);
            this.setPosition(1);
        } else {
            return this.getId();
        }
        if (this.getCeng() == 7) {
            this.setJie(this.getJie() + 1);
            this.setCeng(1);
            this.setPosition(1);
        } else {
            return this.getId();
        }
        if (this.getJie() == 3) {
            this.setDir(this.getDir() + 1);
            this.setCeng(1);
            this.setPosition(1);
            this.setJie(1);
        } else {
            return this.getId();
        }
        if (this.getDir() == 2) {
            this.setLie(this.getLie() + 1);
            this.setDir(1);
            this.setCeng(1);
            this.setPosition(1);
            this.setJie(1);
        } else {
            return this.getId();
        }


        return this.getId();
    }

    public String getString() {
        return this.getRegion() + "区" + this.getLie() + "列" + (this.getDir() == 0 ? "左" : "右") + this.getJie() + "节" + this.getCeng() + "层" + this.getPosition()+"位";
    }

    public boolean isVaild() {
        if (this.getRegion() < 0 ) {
            return false;
        }
        if (this.getLie() < 1 || this.getLie() > 99) {
            return false;
        }
        if (this.getDir() < 0 || this.getDir() > 1) {
            return false;
        }
        if (this.getJie() < 1 || this.getJie() > 9) {
            return false;
        }
        if (this.getCeng() < 1 || this.getCeng() > 9) {
            return false;
        }
        if (this.getPosition() < 1 || this.getPosition() > 99) {
            return false;
        }
        return true;
    }


    public static void main(String[] args) {
        Cellid cellid = new Cellid(10, 12, 1, 1, 1, 1);
        System.out.println(cellid);
        System.out.println(cellid.getId());
    }
}
